package umg.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import umg.Constants;

public class PropertyManager
{

	private static SortedProperties properties;// shared
	private static String proxy_host;
	private static String proxy_port;
	private static String proxy_enabled;
	private static String chat_enabled;

	private static String minimize_to_tray_enabled;
	private static String autokickPlayerEnabled, startMinimizedEnabled;
	private static String autokickProxyUsersEnabled;

	private static String[] webadminURLs;
	private static String[] webadminURLNames;
	private static String webadmin_username;
	private static String webadmin_password;

	private static String driverClassname;
	private static String dburl;
	private static String dbUsername;
	private static String dbPassword;

	private static String grab_interval;
	private static String IPWatchList;
	private static String adminIPAddresses;
	private static String NameWatchList;
	private static String autoKickMessage;
	private static String updateScreen;
	private static String databaseModeOnly = "false";
	private static String autoKickPlayerList;
	private static String showAd = "false";
	private static String showCountries = "false";
	private static String selectedSkin;

	private static boolean debug;
	static HashMap<String, String> autoKickNameHash = null;
	private static String threadPriority;

	public static void setStartMinimizedEnabled(String startMinimizedEnabled)
	{
		PropertyManager.startMinimizedEnabled = startMinimizedEnabled;
	}

	public static String getStartMinimizedEnabled()
	{
		return startMinimizedEnabled;
	}

	public static String getThreadPriority()
	{
		return threadPriority;
	}

	public static int getThreadPriorityInt()
	{
		return Integer.parseInt(threadPriority);
	}

	public static void setThreadPriority(String threadPriority)
	{
		int pri = 0;
		try
		{
			pri = Integer.parseInt(threadPriority);
		}
		catch (Exception e)
		{
		}

		if (pri < 1 || pri > 10)
		{
			PropertyManager.threadPriority = "5";
			System.out.println("Invalid thread priority. Using normal priority of 5.");
		}
		else
		{
			PropertyManager.threadPriority = threadPriority;
		}
	}

	public static boolean isDebug()
	{
		return debug;
	}

	public static void setDebug(boolean debug)
	{
		PropertyManager.debug = debug;
	}

	public static String getAdminIPAddresses()
	{
		return adminIPAddresses;
	}

	public static void setAdminIPAddresses(String adminIPAddresses)
	{
		adminIPAddressesArray = null;
		PropertyManager.adminIPAddresses = adminIPAddresses;
	}

	public static String getAutoKickMessage()
	{
		return autoKickMessage;
	}

	public static void setAutoKickMessage(String autoKickMessage)
	{
		PropertyManager.autoKickMessage = autoKickMessage;
	}

	public static String getAutokickPlayerEnabled()
	{
		return autokickPlayerEnabled;
	}

	public static void setAutokickPlayerEnabled(String autokickPlayerEnabled)
	{
		PropertyManager.autokickPlayerEnabled = autokickPlayerEnabled;
	}

	public PropertyManager()
	{
		if (properties == null)
		{
			// //read all properties once
			if (PropertyManager.isDebug()) System.out.println("Reading Properties...");
			// properties =
			// PropertyLoader.loadProperties(umg.Constants.SETTINGS_FILE);
			properties = new SortedProperties();
			try
			{
				properties.load(new FileInputStream(umg.Constants.SETTINGS_FILE));
			}
			catch (FileNotFoundException e)
			{
				System.out.println("Exception " + e.getMessage());
				e.printStackTrace();
			}
			catch (IOException e)
			{
				System.out.println("Exception " + e.getMessage());
				e.printStackTrace();
			}
			setValues();
			// System.out.println(toString());
		}
		else
		{
			if (PropertyManager.isDebug()) System.out.println("Not re-reading properties");
		}
	}

	private void setValues()
	{
		// setCurrent_players_url(properties.getProperty("current_players_url","http://208.100.57.178:5080/ServerAdmin/current_players?Sort=Score?Last=Score?ReverseSort=True"));
		// read all the 'webadmin_url_*' and 'webadmin_url_*_name'values
		String[] URLs = new String[Constants.MAX_URLS];
		String[] URLNames = new String[Constants.MAX_URLS];
		int i = 0;
		for (; i < Constants.MAX_URLS; i++)
		{
			String url = properties.getProperty("webadmin_url_" + (i + 1));
			String url_name = properties.getProperty("webadmin_url_" + (i + 1) + "_name");
			if (url == null || url.length() < 5)
			{
				break;
			}
			else
			{
				URLs[i] = url;
				if (url_name == null || url_name.length() == 0) URLNames[i] = url.substring(0, url.indexOf("/ServerAdmin"));
				else URLNames[i] = url_name;
			}
		}
		PropertyManager.webadminURLs = new String[i];
		PropertyManager.webadminURLNames = new String[i];
		// copy
		for (int j = 0; j < i; j++)
		{
			PropertyManager.webadminURLs[j] = URLs[j];
			PropertyManager.webadminURLNames[j] = URLNames[j];
		}
		// setWebadminURLs(webadminURLs);
		// setWebadminURLNames(webadminURLNames);
		if (webadminURLs.length < 1)
		{
			System.out.println("Could not read URLs from the setting file.");
		}
		else
		{
			if (PropertyManager.isDebug()) System.out.println("Read " + webadminURLs.length + " URLs");
			if (PropertyManager.isDebug()) System.out.println("Read " + webadminURLNames.length + " URL Names");
		}
		setwebadmin_username(properties.getProperty("webadmin_username", ""));
		setwebadmin_password(properties.getProperty("webadmin_password", ""));
		setGrab_interval(properties.getProperty("grab_interval", "60"));
		setProxyHost(properties.getProperty("proxy_host", "none"));
		setProxy_port(properties.getProperty("proxy_port", "8080"));
		setProxy_enabled(properties.getProperty("proxy_enabled", "false"));
		setChat_enabled(properties.getProperty("chat_enabled", "true"));
		setMinimize_to_tray_enabled(properties.getProperty("minimize_to_tray_enabled", "false"));
		setDriverClassname(properties.getProperty("driver_classname", "com.mysql.jdbc.Driver"));
		setDburl(properties.getProperty("dburl", "jdbc:mysql://localhost:3306/ut2004db"));
		setDbUsername(properties.getProperty("dbUsername", ""));
		setDbPassword(properties.getProperty("dbPassword", ""));
		setIPWatchList(properties.getProperty("ip_watchlist", ""));
		setAdminIPAddresses(properties.getProperty("admin_ip_list", ""));
		setNameWatchList(properties.getProperty("name_watchlist", ""));
		setAutokickPlayerEnabled(properties.getProperty("autokick_player_enabled", "false"));
		setStartMinimizedEnabled(properties.getProperty("start_hidden_enabled", "false"));
		setAutoKickMessage(properties.getProperty("auto_kick_message", "Player get a name. use 'setname <newname>'  or change it in your game settings."));
		setUpdateScreen(properties.getProperty("update_screen", "true"));
		setDatabaseModeOnly(properties.getProperty("database_mode_only", "false"));
		setAutoKickPlayerList(properties.getProperty("auto_kick_player_list", ""));
		setShowAd(properties.getProperty("show_ad", "false").toLowerCase());//default to false for everyone.
		setShowCountries(properties.getProperty("show_countries", "false").toLowerCase());//default to false for everyone.
		String debugValue = properties.getProperty("debug", "false");
		setThreadPriority(properties.getProperty("thread_priority", "5"));// default, range 1..10
		setSelectedSkin(properties.getProperty("skin", "Metal"));//Metal is the Cross platform L&F
		setAutokickProxyUsersEnabled(properties.getProperty("auto_kick_proxy_users", "false"));

		if (debugValue.equalsIgnoreCase("true")) setDebug(true);
	}

	public String toString()
	{
		StringBuilder str = new StringBuilder();
		str.append("Properties read:");
		str.append("\n");
		str.append(getWebadminURLs());
		str.append("\n");
		str.append(getWebadminURLNames());
		str.append("\n");
		str.append(getwebadmin_username());
		str.append("\n");
		str.append(getwebadmin_password());
		str.append("\n");
		str.append(getGrab_interval());
		str.append("\n");
		str.append(getProxyHost());
		str.append("\n");
		str.append(getProxy_port());
		str.append("\n");
		str.append(getProxy_enabled());
		str.append("\n");
		str.append(getAutokickPlayerEnabled());
		str.append("\n");
		str.append(getMinimize_to_tray_enabled());
		str.append("\n");
		str.append(getDriverClassname());
		str.append("\n");
		str.append(getDburl());
		str.append("\n");
		str.append(getAutoKickMessage());
		str.append("\n");
		str.append(getAdminIPAddresses());
		str.append("\n");
		return str.toString();
	}

	public static String getProxyHost()
	{
		return proxy_host;
	}

	public static void setProxyHost(String proxy_host)
	{
		PropertyManager.proxy_host = proxy_host;
	}

	public static String getProxy_port()
	{
		return proxy_port;
	}

	public static void setProxy_port(String proxy_port)
	{
		PropertyManager.proxy_port = proxy_port;
	}

	public static String getProxy_enabled()
	{
		return proxy_enabled;
	}

	public static void setProxy_enabled(String proxy_enabled)
	{
		PropertyManager.proxy_enabled = proxy_enabled;
	}

	public static String getwebadmin_username()
	{
		return webadmin_username;
	}

	public static void setwebadmin_username(String webadmin_username)
	{
		PropertyManager.webadmin_username = webadmin_username;
	}

	public static String getwebadmin_password()
	{
		return webadmin_password;
	}

	public static void setwebadmin_password(String webadmin_password)
	{
		PropertyManager.webadmin_password = webadmin_password;
	}

	public static String getGrab_interval()
	{
		if (Integer.parseInt(grab_interval) < 15)// dont kill the server
		{
			grab_interval = "15";
		}
		return grab_interval;
	}

	public static void setGrab_interval(String interval)
	{
		if (Integer.parseInt(interval) < 15)// dont kill the server
		{
			grab_interval = "15";
		}
		PropertyManager.grab_interval = interval;
	}

	public static void saveToDisk()
	{
		properties.setProperty("grab_interval", getGrab_interval());
		properties.setProperty("proxy_enabled", getProxy_enabled());
		properties.setProperty("chat_enabled", getChat_enabled());
		properties.setProperty("autokick_player_enabled", getAutokickPlayerEnabled());
		properties.setProperty("start_hidden_enabled", getStartMinimizedEnabled());
		properties.setProperty("minimize_to_tray_enabled", getMinimize_to_tray_enabled());
		properties.setProperty("webadmin_username", getwebadmin_username());
		properties.setProperty("webadmin_password", getwebadmin_password());
		properties.setProperty("ip_watchlist", getIPWatchList());
		properties.setProperty("name_watchlist", getNameWatchList());
		properties.setProperty("auto_kick_message", getAutoKickMessage());
		properties.setProperty("admin_ip_list", getAdminIPAddresses());
		properties.setProperty("update_screen", getUpdateScreen());
		properties.setProperty("database_mode_only", getDatabaseModeOnly());
		properties.setProperty("auto_kick_player_list", getAutoKickPlayerList());
		properties.setProperty("show_ad", getShowAd());
		properties.setProperty("show_countries", getShowCountries());
		properties.setProperty("debug", isDebug() == true ? "true" : "false");
		properties.setProperty("thread_priority", getThreadPriority());
		properties.setProperty("skin", getSelectedSkin());
		properties.setProperty("auto_kick_proxy_users", getAutokickProxyUsersEnabled());

		FileOutputStream out;
		try
		{
			out = new FileOutputStream(umg.Constants.SETTINGS_FILE);
			properties.store(out, "This file is written to automatically. ");
			out.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		System.out.println("Saved Properties.");

	}

	public static String getDriverClassname()
	{
		return driverClassname;
	}

	public static void setDriverClassname(String driverClassName)
	{
		PropertyManager.driverClassname = driverClassName;
	}

	public static String getDburl()
	{
		return dburl;
	}

	public static void setDburl(String url)
	{
		PropertyManager.dburl = url;
	}

	public static String[] getWebadminURLs()
	{
		return webadminURLs;
	}

	public static void setWebadminURLs(String[] webadminURLs)
	{
		PropertyManager.webadminURLs = webadminURLs;
	}

	public static String[] getWebadminURLNames()
	{
		return webadminURLNames;
	}

	public static void setWebadminURLNames(String[] webadminURLNames)
	{
		PropertyManager.webadminURLNames = webadminURLNames;
	}

	public static String getMinimize_to_tray_enabled()
	{
		return minimize_to_tray_enabled;
	}

	public static void setMinimize_to_tray_enabled(String minimize_to_tray_enabled)
	{
		PropertyManager.minimize_to_tray_enabled = minimize_to_tray_enabled;
	}

	public static String getIPWatchList()
	{
		return IPWatchList;
	}

	public static void setIPWatchList(String watchList)
	{
		ipWatchesArray = null;
		IPWatchList = watchList;
	}

	public static String getNameWatchList()
	{
		return NameWatchList;
	}

	//static to save memory, its set to null by each setter
	static String[] ipWatchesArray;
	static String[] nameWatchesArray;
	static String[] adminIPAddressesArray;

	public static String[] getNameWatchListArray()
	{
		if (nameWatchesArray == null) nameWatchesArray = getNameWatchList().split(Constants.NAME_LIST_SEPERATOR);
		return nameWatchesArray;
	}

	public static String[] getIPWatchListArray()
	{
		if (ipWatchesArray == null) ipWatchesArray = PropertyManager.getIPWatchList().split(Constants.IP_LIST_SEPERATOR);
		return ipWatchesArray;
	}

	public static String[] getAdminIPAddressesArray()
	{
		if (adminIPAddressesArray == null) adminIPAddressesArray = getAdminIPAddresses().split(Constants.IP_LIST_SEPERATOR);
		return adminIPAddressesArray;
	}

	public static void setNameWatchList(String nameWatchList)
	{
		nameWatchesArray = null;
		NameWatchList = nameWatchList;
	}

	public static String getChat_enabled()
	{
		return chat_enabled;
	}

	public static void setChat_enabled(String chat_enabled)
	{
		PropertyManager.chat_enabled = chat_enabled;
	}

	public static void setUpdateScreen(String updateScreen)
	{
		PropertyManager.updateScreen = updateScreen;
	}

	public static String getUpdateScreen()
	{
		return updateScreen;
	}

	public static void setAutoKickPlayerList(String autoKickPlayerList)
	{
		PropertyManager.autoKickPlayerList = autoKickPlayerList;
	}

	public static String getAutoKickPlayerList()
	{
		return autoKickPlayerList;
	}

	public static String getDatabaseModeOnly()
	{
		return databaseModeOnly;
	}

	public static void setDatabaseModeOnly(String databaseMode)
	{
		PropertyManager.databaseModeOnly = databaseMode;
	}

	public static void setShowAd(String showAd)
	{
		PropertyManager.showAd = showAd;
	}

	public static String getShowAd()
	{
		return showAd;
	}

	public static void setShowCountries(String showCountries)
	{
		PropertyManager.showCountries = showCountries;
	}

	public static String getShowCountries()
	{
		return showCountries;
	}

	public static void setSelectedSkin(String selectedSkin)
	{
		PropertyManager.selectedSkin = selectedSkin;
	}

	public static String getSelectedSkin()
	{
		return selectedSkin;
	}

	public static void setAutokickProxyUsersEnabled(String autokickProxyUsersEnabled)
	{
		PropertyManager.autokickProxyUsersEnabled = autokickProxyUsersEnabled;
	}

	public static String getAutokickProxyUsersEnabled()
	{
		return autokickProxyUsersEnabled;
	}

	public static String getDbUsername() {
		return dbUsername;
	}

	public static void setDbUsername(String dbUsername) {
		PropertyManager.dbUsername = dbUsername;
	}

	public static String getDbPassword() {
		return dbPassword;
	}

	public static void setDbPassword(String dbPassword) {
		PropertyManager.dbPassword = dbPassword;
	}
}